package com.ordos.recruit.module.apply.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ordos.recruit.module.apply.entity.*;
import com.ordos.recruit.module.apply.mapper.RecruitApplyMapper;
import com.ordos.recruit.module.apply.mapper.ViewRecruitApplyMapper;
import com.ordos.recruit.module.audit.entity.RecruitmentApplicationAuditPo;
import com.ordos.recruit.module.audit.mapper.ApplyAuditMapper;
import com.ordos.recruit.module.batch.entity.RecruitBatchApplyPo;
import com.ordos.recruit.module.batch.mapper.RecruitBatchApplyMapper;
import com.ordos.recruit.module.batch.mapper.StudyExperienceMapper;
import com.ordos.recruit.module.batch.mapper.WorkExperienceMapper;
import com.ordos.recruit.module.user.entity.SysSsoUserDto;
import com.ordos.recruit.module.user.util.UserUtil;
import com.wonders.commons.util.response.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * Service
 *
 * @author jin
 */
@Service
public class RecruitApplyService {

    private final RecruitApplyMapper recruitApplyMapper;
    private final StudyExperienceMapper studyExperienceMapper;
    private final WorkExperienceMapper workExperienceMapper;
    private final RecruitBatchApplyMapper recruitBatchApplyMapper;
    private final ViewRecruitApplyMapper viewRecruitApplyMapper;
    private final ApplyAuditMapper applyAuditMapper;

    public RecruitApplyService(RecruitApplyMapper recruitApplyMapper, StudyExperienceMapper studyExperienceMapper, WorkExperienceMapper workExperienceMapper, RecruitBatchApplyMapper recruitBatchApplyMapper, ViewRecruitApplyMapper viewRecruitApplyMapper, ApplyAuditMapper applyAuditMapper) {
        this.recruitApplyMapper = recruitApplyMapper;
        this.studyExperienceMapper = studyExperienceMapper;
        this.workExperienceMapper = workExperienceMapper;
        this.recruitBatchApplyMapper = recruitBatchApplyMapper;
        this.viewRecruitApplyMapper = viewRecruitApplyMapper;
        this.applyAuditMapper = applyAuditMapper;
    }

    /**
     * 保存或更新申请表.
     *
     * @param recruitApplyDto 请求入参.
     */
    public ApiResponse saveOrUpdate(RecruitApplyDto recruitApplyDto) {
        QueryWrapper<RecruitmentApplicationAuditPo> applyAuditQueryWrapper = new QueryWrapper<>();
        applyAuditQueryWrapper.eq("APPLICATION_ID", recruitApplyDto.getId());
        RecruitmentApplicationAuditPo recruitmentApplicationAuditPo = applyAuditMapper.selectOne(applyAuditQueryWrapper);
        if(recruitmentApplicationAuditPo != null && ("0".equals(recruitmentApplicationAuditPo.getAuditStatusCode()) || "1".equals(recruitmentApplicationAuditPo.getAuditStatusCode()))){
            return ApiResponse.error("审核通过或是审核不通过的招聘申报不可以再进行修改");
        }
        RecruitBatchApplyPo recruitBatchApplyPo = recruitBatchApplyMapper.selectById(recruitApplyDto.getAppliedNumber());
        if (recruitBatchApplyPo == null) {
            return ApiResponse.error("招聘申报批次号不存在");
        }
        SysSsoUserDto sysSsoUserDto = UserUtil.getUserByRequest();
        RecruitApplyPo recruitApplyPo = new RecruitApplyPo();
        if (StringUtils.isBlank(recruitApplyDto.getId())) {
            QueryWrapper<RecruitApplyPo> recruitApplyQuery = new QueryWrapper<RecruitApplyPo>();
            recruitApplyQuery.eq("APPLIED_NUMBER", recruitApplyDto.getAppliedNumber());
            recruitApplyQuery.eq("CREATE_USER", sysSsoUserDto.getUserid());
            List<RecruitApplyPo> list = recruitApplyMapper.selectList(recruitApplyQuery);
            if (list != null && !list.isEmpty()) {
                return ApiResponse.error("每人只能申请一次");
            }

            String recruitApplyId = UUID.randomUUID().toString().replace("-", "");
            BeanUtil.copyProperties(recruitApplyDto, recruitApplyPo);
            recruitApplyPo.setId(recruitApplyId);
            recruitApplyPo.setCreateUser(sysSsoUserDto.getUserid());
            recruitApplyPo.setUpdateTime(new Date());
            recruitApplyPo.setUpdateUser(sysSsoUserDto.getUserid());
            recruitApplyMapper.insert(recruitApplyPo);

            String studyExperienceId = UUID.randomUUID().toString().replace("-", "");
            List<StudyExperienceDto> studyExperienceDtoList = recruitApplyDto.getStudyExperienceList();
            StudyExperiencePo studyExperiencePo = null;
            StudyExperienceDto studyExperienceDto = null;
            for (StudyExperienceDto experienceDto : studyExperienceDtoList) {
                studyExperienceDto = experienceDto;
                studyExperiencePo = new StudyExperiencePo();
                BeanUtil.copyProperties(studyExperienceDto, studyExperiencePo);
                studyExperiencePo.setId(studyExperienceId);
                studyExperiencePo.setApplicationId(recruitApplyId);
                studyExperiencePo.setCreateUser(sysSsoUserDto.getUserid());
                studyExperiencePo.setUpdateTime(new Date());
                studyExperiencePo.setUpdateUser(sysSsoUserDto.getUserid());
                studyExperienceMapper.insert(studyExperiencePo);
            }

            String workExperienceId = UUID.randomUUID().toString().replace("-", "");
            List<WorkExperienceDto> workExperienceDtoList = recruitApplyDto.getWorkExperienceList();
            WorkExperiencePo workExperiencePo = null;
            WorkExperienceDto workExperienceDto = null;
            for (WorkExperienceDto experienceDto : workExperienceDtoList) {
                workExperienceDto = experienceDto;
                workExperiencePo = new WorkExperiencePo();
                BeanUtil.copyProperties(workExperienceDto, workExperiencePo);
                workExperiencePo.setId(workExperienceId);
                workExperiencePo.setApplicationId(recruitApplyId);
                workExperiencePo.setCreateUser(sysSsoUserDto.getUserid());
                workExperiencePo.setUpdateTime(new Date());
                workExperiencePo.setUpdateUser(sysSsoUserDto.getUserid());
                workExperienceMapper.insert(workExperiencePo);
            }
        } else {
            BeanUtil.copyProperties(recruitApplyDto, recruitApplyPo);
            recruitApplyPo.setUpdateTime(new Date());
            recruitApplyPo.setUpdateUser(sysSsoUserDto.getUserid());
            recruitApplyPo.setCandidateName(null);
            recruitApplyPo.setIdNumber(null);
            recruitApplyPo.setMobilePhone(null);
            recruitApplyPo.setEmergencyContactName(null);
            recruitApplyPo.setEmergencyContactPhone(null);
            recruitApplyMapper.updateById(recruitApplyPo);

            QueryWrapper<StudyExperiencePo> studyExperienceQuery = new QueryWrapper<>();
            studyExperienceQuery.eq("APPLICATION_ID", recruitApplyPo.getId());
            studyExperienceMapper.delete(studyExperienceQuery);
            String studyExperienceId = UUID.randomUUID().toString().replace("-", "");
            List<StudyExperienceDto> studyExperienceDtoList = recruitApplyDto.getStudyExperienceList();
            StudyExperiencePo studyExperiencePo = null;
            StudyExperienceDto studyExperienceDto = null;
            for (StudyExperienceDto experienceDto : studyExperienceDtoList) {
                studyExperienceDto = experienceDto;
                studyExperiencePo = new StudyExperiencePo();
                BeanUtil.copyProperties(studyExperienceDto, studyExperiencePo);
                studyExperiencePo.setId(studyExperienceId);
                studyExperiencePo.setApplicationId(recruitApplyPo.getId());
                studyExperiencePo.setCreateUser(sysSsoUserDto.getUserid());
                studyExperiencePo.setUpdateTime(new Date());
                studyExperiencePo.setUpdateUser(sysSsoUserDto.getUserid());
                studyExperienceMapper.insert(studyExperiencePo);
            }

            QueryWrapper<WorkExperiencePo> workExperienceQuery = new QueryWrapper<>();
            workExperienceQuery.eq("APPLICATION_ID", recruitApplyPo.getId());
            workExperienceMapper.delete(workExperienceQuery);
            String workExperienceId = UUID.randomUUID().toString().replace("-", "");
            List<WorkExperienceDto> workExperienceDtoList = recruitApplyDto.getWorkExperienceList();
            WorkExperiencePo workExperiencePo = null;
            WorkExperienceDto workExperienceDto = null;
            for (WorkExperienceDto experienceDto : workExperienceDtoList) {
                workExperienceDto = experienceDto;
                workExperiencePo = new WorkExperiencePo();
                BeanUtil.copyProperties(workExperienceDto, workExperiencePo);
                workExperiencePo.setId(workExperienceId);
                workExperiencePo.setApplicationId(recruitApplyPo.getId());
                workExperiencePo.setCreateUser(sysSsoUserDto.getUserid());
                workExperiencePo.setUpdateTime(new Date());
                workExperiencePo.setUpdateUser(sysSsoUserDto.getUserid());
                workExperienceMapper.insert(workExperiencePo);
            }
        }
        return ApiResponse.success();
    }

    /**
     * 查询一条数据.
     *
     * @param recruitApplyDto 请求入参.
     * @return Po
     */
    public RecruitApplyPo view(RecruitApplyDto recruitApplyDto) {
        SysSsoUserDto sysSsoUserDto = UserUtil.getUserByRequest();
        QueryWrapper<RecruitApplyPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CREATE_USER", sysSsoUserDto.getUserid());
        queryWrapper.eq("ID", recruitApplyDto.getId());
        RecruitApplyPo recruitApplyPo = recruitApplyMapper.selectOne(queryWrapper);

        QueryWrapper<StudyExperiencePo> studyExperienceQuery = new QueryWrapper<>();
        studyExperienceQuery.eq("APPLICATION_ID", recruitApplyPo.getId());
        studyExperienceQuery.eq("CREATE_USER", sysSsoUserDto.getUserid());
        List<StudyExperiencePo> studyExperienceList = studyExperienceMapper.selectList(studyExperienceQuery);
        recruitApplyPo.setStudyExperienceList(studyExperienceList);

        QueryWrapper<WorkExperiencePo> workExperienceQuery = new QueryWrapper<>();
        workExperienceQuery.eq("APPLICATION_ID", recruitApplyPo.getId());
        workExperienceQuery.eq("CREATE_USER", sysSsoUserDto.getUserid());
        List<WorkExperiencePo> workExperienceList = workExperienceMapper.selectList(workExperienceQuery);
        recruitApplyPo.setWorkExperienceList(workExperienceList);

        return recruitApplyPo;
    }

    /**
     * 查询一条数据.
     *
     * @param recruitApplyDto 请求入参.
     * @return Po
     */
    public ViewRecruitApplyPo allView(RecruitApplyDto recruitApplyDto) {
        QueryWrapper<ViewRecruitApplyPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", recruitApplyDto.getId());
        ViewRecruitApplyPo viewRecruitApplyPo = viewRecruitApplyMapper.selectOne(queryWrapper);

        QueryWrapper<StudyExperiencePo> studyExperienceQuery = new QueryWrapper<>();
        studyExperienceQuery.eq("APPLICATION_ID", viewRecruitApplyPo.getId());
        List<StudyExperiencePo> studyExperienceList = studyExperienceMapper.selectList(studyExperienceQuery);
        viewRecruitApplyPo.setStudyExperienceList(studyExperienceList);

        QueryWrapper<WorkExperiencePo> workExperienceQuery = new QueryWrapper<>();
        workExperienceQuery.eq("APPLICATION_ID", viewRecruitApplyPo.getId());
        List<WorkExperiencePo> workExperienceList = workExperienceMapper.selectList(workExperienceQuery);
        viewRecruitApplyPo.setWorkExperienceList(workExperienceList);

        return viewRecruitApplyPo;
    }

    /**
     * 查询列表数据.
     *
     * @param recruitApplyDto 请求入参.
     * @return Po
     */
    public List<Map<String, String>> getList(RecruitApplyDto recruitApplyDto) {
        SysSsoUserDto sysSsoUserDto = UserUtil.getUserByRequest();
        return recruitApplyMapper.getList(sysSsoUserDto.getUserid());
    }


    /**
     * 查询列表数据.
     *
     * @param speRecruitApplyDto 请求入参.
     * @return Po
     */
    public IPage<SpeRecruitApplyPo> getAllList(SpeRecruitApplyDto speRecruitApplyDto) {
        Page<SpeRecruitApplyPo> selectPage = new Page<>(speRecruitApplyDto.getPage(), speRecruitApplyDto.getSize());
        List<SpeRecruitApplyPo> speRecruitApplyList = recruitApplyMapper.getAllList(selectPage,speRecruitApplyDto);
        selectPage.setRecords(speRecruitApplyList);
        return selectPage;
    }
}
